package com.gmu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblStudentMapper;
import com.gmu.mapper.TblTeacherMapper;
import com.gmu.pojo.TblStudent;
import com.gmu.pojo.TblTeacher;
import com.gmu.pojo.TblUser;
import com.gmu.service.TblTeacherService;
import com.gmu.service.TblUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 教师信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@RestController
@Api(tags = "教师信息表")
@RequestMapping("/gmu/tbl-teacher")
public class TblTeacherController {

    @Resource
    private TblTeacherService teacherService;

    @Resource
    private TblUserService userService;

    @Autowired
    private TblTeacherMapper tblTeacherMapper;

    //查询所有老师信息
    @GetMapping("listAll")
    @ApiOperation("所有老师信息")
    public ResResult listAll(){
        List<TblTeacher> teacherList = teacherService.list(null);
        return new ResResult(200,"查询所有老师信息成功",teacherList);
    }

    //添加或修改教师的方法
    @PostMapping("addTeacher")
    @ApiOperation("添加和修改教师信息")
    public ResResult addTeacher(@RequestBody TblTeacher tblTeacher){
       QueryWrapper<TblUser> wrapper = new QueryWrapper<>();
       wrapper.eq("id_number",tblTeacher.getIdNumber());
        TblUser tblUser = userService.getOne(wrapper);
        if(tblUser==null){
            return new ResResult(500,"该身份证暂未注册，请先注册");
        }
        Integer userId = tblUser.getUserId();
        tblTeacher.setUserId(userId);
        boolean save = teacherService.save(tblTeacher);
        if(save){
            return new ResResult(200,"执行成功");
        }else{
            return new ResResult(500,"执行失败");
        }
    }

    //根据id查询学生信息
    //用于修改前的数据回显
    @GetMapping("byIdTeacher/{teacherId}")
    @ApiOperation("根据id查询教师信息")
    public ResResult byIdTeacher(@PathVariable Integer teacherId){
        TblTeacher teacherById = teacherService.getById(teacherId);
        return new ResResult(200,"查询成功",teacherById);
    }

    //根据id删除教师信息
    @DeleteMapping("deleteTeacher/{teacherId}")
    @ApiOperation("删除教师信息")
    public ResResult deleteTeacher(@PathVariable Integer teacherId){
        Integer userId = teacherService.getById(teacherId).getUserId();
        boolean removeById = teacherService.removeById(teacherId);
        userService.removeById(userId);
        if(removeById){
            return new ResResult(200,"删除成功");
        }else{
            return new ResResult(500,"删除失败");
        }
    }

    // 分页查询讲师的方法
    @ApiOperation("分页查询")
    @GetMapping("pageTeacher/{current}/{limit}")
    public ResResult pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {
        //创建page对象
        Page<TblTeacher> pageTeacher = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();//总记录数
        List<TblTeacher> records = pageTeacher.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return new ResResult(200,map);
    }

    @ApiOperation(value = "搜索")
    @PostMapping("/getTeacherByCondition")
    public ResResult<?>getTblTeacherByCondition(@RequestBody TblTeacher tblTeacher){
        return teacherService.getTblTeacherByCondition(tblTeacher);
    }

    @ApiOperation(value = "批量删除老师信息")
    @DeleteMapping("/deleteUserByIds/{userIds}")
    public ResResult<?> deleteUserByIds(@PathVariable Integer[] userIds){
        Integer deleteTotal = tblTeacherMapper.deleteUserByIds(userIds);
        if (deleteTotal < 1){
            return new ResResult<>(500,"删除失败");
        }
        return new ResResult<>(200,"本次删除" + deleteTotal + "条记录");
    }
}

